using System;
using System.Diagnostics;
using System.Collections.Generic;
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
	/// <summary>
	/// Author: Zhang Andy
	/// </summary>
    class SVNUpdate : CommandBase
    {
        // Initialise variables with default settings.
        private long revision_no;
        private java.io.File wc_filepath;
        private SVNRevision revision = SVNRevision.HEAD;
        private SVNDepth depth = SVNDepth.INFINITY;
        private bool allowUnversionedObstruc = true;
        private bool depthIsSticky = true;

        // Minimal Constructor
		public SVNUpdate(string filepath)
        {
			Debug.WriteLine("SVNUpdate minimal Constructor with defaults");
            this.wc_filepath = new java.io.File(filepath);
		}

        public SVNUpdate(string filepath, string revisionNo)
        {
            Debug.WriteLine("SVNUpdate minimal Constructor with defaults");
            this.wc_filepath = new java.io.File(filepath);
            this.revision = SVNRevision.parse(revisionNo);
        }

        // Full Constructor
        public SVNUpdate(string filepath, SVNRevision rev, SVNDepth dep, bool allowobs, bool depthSticky)
        {
            Debug.WriteLine("SVNUpdate full Constructor");
            
            this.wc_filepath = new java.io.File(filepath);
            this.revision = rev;
            this.depth = dep;
            this.allowUnversionedObstruc = allowobs;
            this.depthIsSticky = depthSticky;
        }

        public override void Execute()
        {
            Debug.WriteLine("SVNUpdate Execution");

            //library setup
            Tools.SetupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();

            try
            {
                revision_no = cm.getUpdateClient().doUpdate(wc_filepath, revision,
                        depth, allowUnversionedObstruc, depthIsSticky);
                Console.WriteLine("Updated to revision {0}", revision_no);
            }
            catch (SVNException e)
            {
                throw new SVNExecuteException(e.getMessage());
            }
        }
    }
}
